import * as YvanUI from "yvan-ui-wotu";
import * as Data from "../../data";

const _ = YvanUI.lodash

interface Ref {
    theform: YvanUI.Form
}

export default class NodeAttributeInsert extends YvanUI.Scope<void, Ref> {

    main = this.$$({
        type: "disease",
        version: Data.defaultVersion,
        name: "",              // 名称
        introduction: "",      // 百科
        treatment: "",         // 治疗
        symptom: "",           // 症状
        ban: "",               // 禁忌
        adverse: "",           // 不良反应
        precaution: "",        // 注意事项
        usage: "",             // 用法用量
    })

    onRender() {
        window['biz'] = this
    }

    vjson = {
        title: '病药百科',
        items: [
            {
                view: 'card',
                items: [
                    {
                        view: 'form',
                        ref: 'theform',
                        labelCol: {span: 6},
                        wrapperCol: {span: 14},
                        items: [
                            {
                                view: 'select',
                                bind: 'main.version',
                                required: true,
                                label: '版本',
                                data: _.map(Data.versions, (n) => {
                                    return {value: n, label: n}
                                })
                            },
                            {
                                view: 'radio',
                                bind: 'main.type',
                                label: '类型',
                                required: true,
                                optionType: 'button',
                                data: [
                                    {value: 'disease', label: '疾病'},
                                    {value: 'medicine', label: '药品'},
                                ]
                            },
                            {
                                view: 'text',
                                bind: 'main.name',
                                required: true,
                                label: '名称'
                            },
                            {
                                view: 'textarea',
                                bind: 'main.introduction',
                                visible: {$watch: () => this.main.type === 'disease'},
                                label: '百科'
                            },
                            {
                                view: 'textarea',
                                bind: 'main.treatment',
                                visible: {$watch: () => this.main.type === 'disease'},
                                label: '治疗'
                            },
                            {
                                view: 'textarea',
                                bind: 'main.symptom',
                                visible: {$watch: () => this.main.type === 'disease'},
                                label: '症状'
                            },
                            {
                                view: 'textarea',
                                bind: 'main.ban',
                                visible: {$watch: () => this.main.type === 'medicine'},
                                label: '禁忌'
                            },
                            {
                                view: 'textarea',
                                bind: 'main.adverse',
                                visible: {$watch: () => this.main.type === 'medicine'},
                                label: '不良反应'
                            },
                            {
                                view: 'textarea',
                                bind: 'main.precaution',
                                visible: {$watch: () => this.main.type === 'medicine'},
                                label: '注意事项'
                            },
                            {
                                view: 'textarea',
                                bind: 'main.usage',
                                visible: {$watch: () => this.main.type === 'medicine'},
                                label: '用法用量'
                            },
                        ]
                    }
                ]
            }
        ],
        footer: [
            {
                view: 'button',
                text: '确定',
                type: 'primary',
                icon: 'fa fa-save',
                onClick: () => new Promise(resolve => {
                    this.refs.theform.validateFields().then(() => {
                        let params = {"name": this.main.name, "version": this.main.version}
                        if (this.main.type === 'disease') {
                            if (this.main.introduction != "" && this.main.introduction != undefined) {
                                params["introduction"] = this.main.introduction
                            }

                            if (this.main.symptom != "" && this.main.symptom != undefined) {
                                params["symptom"] = this.main.symptom
                            }

                            if (this.main.treatment != "" && this.main.treatment != undefined) {
                                params["treatment"] = this.main.treatment
                            }
                        } else {
                            if (this.main.ban != '' && this.main.ban != undefined) {
                                params["ban"] = this.main.ban
                            }

                            if (this.main.adverse != '' && this.main.adverse != undefined) {
                                params["adverse"] = this.main.adverse
                            }

                            if (this.main.precaution != "" && this.main.precaution != undefined) {
                                params["precaution"] = this.main.precaution
                            }

                            if (this.main.usage != "" && this.main.usage != undefined) {
                                params["usage"] = this.main.usage
                            }
                        }

                        return YvanUI.ajax.func({
                            url: '/kg/insert/attribute',
                            method: 'post-json',
                            data: {
                                "label": this.main.type,
                                "params": params
                            }

                        }).then((rest) => {
                            const res: any = rest
                            if (res.code != '0') {
                                YvanUI.showErrorDialog(res.result)

                            } else {
                                YvanUI.showSuccess('插入成功')
                                Object.assign(this.main, {
                                    name: '',
                                    introduction: '',
                                    treatment: '',
                                    symptom: '',
                                    ban: '',
                                    adverse: '',
                                    precaution: '',
                                    usage: '',
                                });
                            }
                        }).finally(()=>{
                            resolve();
                        })

                    }).catch(e => {
                        YvanUI.showValidateErrorDialog(this.refs.theform, e)
                        resolve()
                    })
                })
            },
            {
                view: 'button',
                text: '清空',
                icon: 'fa fa-times',
                onClick: () => {
                    this.refs.theform.resetFields()
                }
            }
        ],
    }
}